package idv.loveloser.servlet;

import idv.loveloser.dao.MdlCourseModules;
import idv.loveloser.dao.MdlCourseModulesDAO;
import idv.loveloser.dao.MdlCourseSections;
import idv.loveloser.dao.MdlCourseSectionsDAO;
import idv.loveloser.dao.MdlFilesDAO;
import idv.loveloser.dao.MdlFolder;
import idv.loveloser.dao.MdlFolderDAO;
import idv.loveloser.dao.MdlForum;
import idv.loveloser.dao.MdlForumDAO;
import idv.loveloser.dao.MdlLabel;
import idv.loveloser.dao.MdlLabelDAO;
import idv.loveloser.dao.MdlModules;
import idv.loveloser.dao.MdlModulesDAO;
import idv.loveloser.dao.MdlPage;
import idv.loveloser.dao.MdlPageDAO;
import idv.loveloser.dao.MdlResource;
import idv.loveloser.dao.MdlResourceDAO;
import idv.loveloser.dao.MdlUrl;
import idv.loveloser.dao.MdlUrlDAO;
import idv.loveloser.util.Util;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

public class GetCourseContent extends HttpServlet {
	private MdlModulesDAO modulesDAO = new MdlModulesDAO();
	private MdlCourseModulesDAO courseModulesDAO = new MdlCourseModulesDAO();
	private MdlFolderDAO folderDAO = new MdlFolderDAO();
	private MdlUrlDAO urlDAO = new MdlUrlDAO();
	private MdlForumDAO forumDAO= new MdlForumDAO();
	private MdlCourseSectionsDAO sectionDAO=new MdlCourseSectionsDAO();
	private List<CourseContent> contents=null;
	/**
	 * Constructor of the object.
	 */
	public GetCourseContent() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		contents=new ArrayList<CourseContent>();
		response.setCharacterEncoding("UTF-8");
		String client = request.getParameter("client");
		String sessionKey = request.getParameter("sessionKey");
		String courseId=request.getParameter("courseId");
		//response.setContentType("application/json");
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		
//		List<MdlModules> modules =modulesDAO.findAll();
		if(Util.isAuthorised(client, sessionKey)){
			List<MdlCourseModules> courseModules=courseModulesDAO.findByCourse(Long.parseLong(courseId));
			for(int index=0;index<courseModules.size();index++){
				MdlCourseModules module=courseModules.get(index);
				if(module.getModule()==6){
					List<MdlFolder> folders=folderDAO.findByCourse(Long.parseLong(courseId));
					for(int index2=0;index2<folders.size();index2++){
						MdlFolder folder =folders.get(index2);
						CourseContent cc = new CourseContent();
						cc.setContent(folder.getIntro());
						cc.setContentType("folder");
						cc.setContentTypeId(6L);
						cc.setModuleId(module.getId());
						cc.setCourseId(Long.parseLong(courseId));
						cc.setSection(module.getSection());
						
						MdlCourseSections section=sectionDAO.findById(module.getSection());
						cc.setSequence(Long.parseLong(section.getSequence()));
						contents.add(cc);
					}
				}
				if(module.getModule()==18){
					List<MdlUrl> urls = urlDAO.findByCourse(Long.parseLong(courseId));
					for(int index2=0;index2<urls.size();index2++){
						MdlUrl url = urls.get(index2);
						CourseContent cc =new CourseContent();
						cc.setContent(url.getIntro());
						cc.setContentType("url");
						cc.setContentTypeId(18L);
						cc.setModuleId(module.getId());
						cc.setSection(module.getSection());
						MdlCourseSections section=sectionDAO.findById(module.getSection());
						cc.setSequence(Long.parseLong(section.getSequence()));
						contents.add(cc);
					}
				}
				if(module.getModule()==7){
					List<MdlForum> forums=forumDAO.findByCourse(Long.parseLong(courseId));
					for(int index2=0;index2<forums.size();index2++){
						MdlForum forum = forums.get(index2);
						CourseContent cc= new CourseContent();
						cc.setContent(forum.getIntro());
						cc.setContentType("forum");
						cc.setContentTypeId(7L);
						cc.setModuleId(module.getId());
						cc.setSection(module.getSection());
						MdlCourseSections section=sectionDAO.findById(module.getSection());
						cc.setSequence(Long.parseLong(section.getSequence()));
						contents.add(cc);
					}
				}
			}
		}
		Gson gson = new Gson();
		
		out.println(gson.toJson(contents));
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
